<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/4/22  16:46
// +----------------------------------------------------------------------
namespace Admin\Controller;


class CardDepositController extends CashierBaseController{

    function index(){
        $type=M("Deposit_type")->select();
        $this->assign("type",$type);
        $this->display();
    }
    //充值
    function doDeposit(){
        $data=I("post.");
        //支付订单生成
        $dt=M("Deposit_type")->where(["type_id"=>$data['type_id']])->find();
        $summary="线下会员充值,名称:".$dt['name'].",延期时间:".$dt['month']."个月,金额:".$dt['price'];
        $Pay=M("Pay");
        $map_pay=[
            "user_id"=>$data['user_id'],
            "status"=>1,
            "type"=>3,
            "way"=>$data['pay_way'],
            "summary"=>$summary,
            "money"=>$data['money'],
            "money_real"=>$data['money_real'],
            "money_zero"=>$data['money_zero']
        ];
        $Pay->startTrans();
        $pay_id=$Pay->data($map_pay)->add();
        if(!$pay_id){
            $Pay->rollback();
            $this->error("充值失败，生成支付错误");
        }
        $map_deposit=[
            "card_id"=>$data['card_id'],
            "type_id"=>$data['type_id'],
            "pay_id"=>$pay_id
        ];
        $deposit_id=M("Deposit")->data($map_deposit)->add();
        if(!$deposit_id){
            $Pay->rollback();
            $this->error("充值失败，生成订单错误");
        }
        //更新会员卡资料
        $d=M("Card")->where(["card_id"=>$data['card_id']])->find();
        if(!$d){
            $this->error("此会员卡不存在");
        }
        $map_card=[];
        if (strtotime($d['expire_time']) < time()) {
            $map_card["expire_time"] = date("Y-m-d h:i:s", strtotime("+" . $dt["month"] . "months", time()));
        } else {
            $map_card["expire_time"] = date("Y-m-d h:i:s", strtotime("+" . $dt["month"] . "months", strtotime($d['expire_time'])));
        }
        $map_card["num_activity"] = $d["num_activity"] + $dt["num_activity"];
        $map_card["num_delivery"] = $d["num_delivery"] + $dt["num_delivery"];
        if($d['discount_buy']>$dt['discount_buy']){
            $map_card["discount_buy"] = $dt["discount_buy"];
        }
        if($d['discount_apply']>$dt['discount_apply']){
            $map_card["discount_apply"] = $dt["discount_apply"];
        }
        $r=M("Card")->where(["card_id"=>$data['card_id']])->data($map_card)->save();
        if($r){
            $Pay->commit();
            $this->success("充值成功",U("orderShow","deposit_id=".$deposit_id));
        }else{
            $Pay->rollback();
            $this->success("充值失败，更新会员错误");
        }
    }

    function orderShow(){
        $deposit_id=I("get.deposit_id");
        $d = [];
        $da = M("View_deposit")->where(["deposit_id"=>$deposit_id])->find();
        $dp = M("Pay")->where(["pay_id" => $da['pay_id']])->find();

        $d['order_id'] = $da['deposit_id']; //订单号
        $d['pay_id'] = $da['pay_id']; //支付流水号
        $d['money_b'] = $da['price']; //原价
        $d['type'] ="会员卡充值";
        $d['discount_buy'] = 1; //折扣
        $d['money'] = $dp['money']; //实际价格
        $d['money_real'] = $dp['money_real']; //实收
        $d['money_zero'] = $dp['money_zero']; //找零
        $d['create_time'] = $dp['create_time']; //找零
        $dc = M("View_deposit")->where(["deposit_id"=>$deposit_id])->select();
        $d['list'] = $dc;

        $user_id=$dp['user_id']==''?0:$dp['user_id'];
        $User= new \Common\Model\UserModel();
        $d['card']=$User->getCard($user_id);
        $d['score']=$User->getScore($user_id);

        $this->assign("d" , $d);
        $this->display('Print:orderDeposit');
    }

}